package com.sws.orderMS.command.impl;

import com.sws.orderMS.OrderMSApplication;
import com.sws.orderMS.OrderMSDao;
import com.sws.orderMS.command.SystemCommand;
import com.sws.orderMS.pojo.User;
import com.sws.orderMS.tool.Tool;

public class LoginCommand extends SystemCommand {

	@Override
	public void execute() {
		OrderMSDao dao = new OrderMSDao();
		User currentUser = OrderMSApplication.getInstance().getUserProfile();
		String username = Tool.askUserInputNotNull("username: ");
		String password = Tool.askUserInputNotNull("password: ");
		User user = dao.login(username, password);
		if (user == null) {
			Tool.printMsg("用户名不存在或密码错误！请重新输入");
			return;
		}
		if (currentUser == null || !currentUser.equals(user)) {
			OrderMSApplication.getInstance().setUserProfile(user);
		} else {
			Tool.printMsg("你已经登录过了！");
			return;
		} 
		Tool.printMsg("欢迎你回来, " + user.getName());
	}

}
